import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		colletIcon: "../../img/collectICON.png",
		collectStatus: "no",
		news: {

		},
		name: '',
		code: '',
		newsId: '',
	},
	onLoad(params) {
		var newsId = params.newsId;
		var name = params.name;
		this.setData({
			newsId: newsId,
			name: name,
		});
		this.loadNoticeInfo();
	},

	loadNoticeInfo: function() {
		var newUrl = getApp().globalData.dangjianUrl + "/party/partyNewsInfo";
		var params = {};
		params["newsId"] = this.data.newsId + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "支部新闻详情加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var news = respData.data.news;
					news.publishTime = this.formatDate("yyyy-MM-dd", news.publishTime);
					this.setData({
						"news": news
					});
					this.loadFavor();
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	loadFavor: function() {
		//支部内容是否收藏
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentIsFavor";
		var params = {};
		params["code"] = this.data.newsId + "";
		params["name"] = this.data.name + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "通知公告是否收藏调用失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var isFavor = respData.data.favor.isFavor;
					//默认为收藏的状态
					var colletIcon = "../../img/collectFillICON.png";
					if (isFavor == "false") {
						colletIcon = "../../img/collectICON.png";
					}
					this.setData({
						"colletIcon": colletIcon,
						"isFavor": isFavor
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	favorContent: function(e) {
		//收藏内容
		var status = e.currentTarget.dataset.status;
		var contentName = e.currentTarget.dataset.name;
		status = "false" == status ? 1 : 0;
		var newUrl = getApp().globalData.dangjianUrl + "/member/userContentFavor";
		var params = {};
		params["code"] = this.data.newsId + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["status"] = status + "";
		params["contentName"] = contentName + "";
		params["contentType"] = "2";
		params["columnCode"] = "";
		params = JSON.stringify(params);

		this.httpGet(newUrl, params, "收藏通知公告内容失败").then(
			(respData) => {
				var result = respData.data.result.code;
				var isFavor = "true";
				var colletIcon = "../../img/collectFillICON.png";
				if (1 == result) {
					if (0 == status) {
						colletIcon = "../../img/collectICON.png";
						isFavor = "false";
						this.showPageMsg("tips", "该内容收藏取消成功！");
					} else {
						this.showPageMsg("tips", "该内容收藏成功！");
					}
					this.setData({
						"colletIcon": colletIcon,
						"isFavor": isFavor
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	}
});
